<?php
function hook_login()
{
//    global $RTR;
    $CI = & get_instance();
//    $class = strtolower($RTR->fetch_class());
    
    $CI->load->library('session');
    $is_login = $CI->session->userdata('userinfo');
    if ($is_login === true) {
        return;
    }
    
    $CI->load->helper('cookie');
    if ( $CI->input->cookie('EB_REMEMBER_SESSION_EMAIL', TRUE) &&
            $CI->input->cookie('EB_REMEMBER_SESSION_ID_TOKEN', TRUE) &&
            $CI->input->cookie('EB_REMEMBER_SESSION_SESS_TOKEN', TRUE) ) {    //cookies中存在相关信息才予以登录        
        $CI->load->model('user_model');
        $CI->load->model('user_remember_model');
        $ifExist = $CI->user_model->exist($CI->input->cookie('EB_REMEMBER_SESSION_EMAIL', TRUE));
        if ($ifExist === FALSE) {            
            $session_cookie_email = array(  //清除cookies防止隐患
                'name'   => 'REMEMBER_SESSION_EMAIL',
                'prefix' => 'EB_'
            );
            $session_cookie_identify_token = array(
                'name'   => 'REMEMBER_SESSION_ID_TOKEN',
                'prefix' => 'EB_'
            );
            $session_cookie_session_token = array(
                'name'   => 'REMEMBER_SESSION_SESS_TOKEN',
                'prefix' => 'EB_'
            );
            delete_cookie($session_cookie_email);
            delete_cookie($session_cookie_identify_token);
            delete_cookie($session_cookie_session_token);
            show_error('Email in Cookies is wrong.',500); //cookies中保存的email地址不存在（这很不正常），可能遭到了cookies欺骗之类的攻击
        } else {
            $result = $CI->user_remember_model->check_and_update_saved_session(
                    $CI->input->cookie('EB_REMEMBER_SESSION_EMAIL', TRUE),
                    $CI->input->cookie('EB_REMEMBER_SESSION_ID_TOKEN', TRUE),
                    $CI->input->cookie('EB_REMEMBER_SESSION_SESS_TOKEN', TRUE)
                    );

            if ( $result >= 0 ) {
                $userinfo = $CI->user_model->getUserInfo($CI->input->cookie('EB_REMEMBER_SESSION_EMAIL', TRUE));
                $newdata = array(
                    'userinfo' => $userinfo
                );
                $CI->session->set_userdata($newdata);
                return;   //登录成功
            }
            else {
                
                $session_cookie_email = array(
                'name'   => 'REMEMBER_SESSION_EMAIL',
                'prefix' => 'EB_'
                );
                $session_cookie_identify_token = array(
                    'name'   => 'REMEMBER_SESSION_ID_TOKEN',
                    'prefix' => 'EB_'
                );
                $session_cookie_session_token = array(
                    'name'   => 'REMEMBER_SESSION_SESS_TOKEN',
                    'prefix' => 'EB_'
                );
                delete_cookie($session_cookie_email);
                delete_cookie($session_cookie_identify_token);
                delete_cookie($session_cookie_session_token);
                return;   //登录失败，此处的原因是验证token失败，可能是IDtoken错误或者是SESStoken改变次数过多了。同样清除cookies防止隐患
            }
        }
    }
    else {
        return;   //登录失败，此处的原因是cookies中没有相关信息
    }
    return;
}
?>